from matplotlib import pyplot as plt
import pylab

#设置汉字格式
# sans-serif就是无衬线字体，是一种通用字体族。
# 常见的无衬线字体有 Trebuchet MS, Tahoma, Verdana, Arial, Helvetica,SimHei 中文的幼圆、隶书等等
pylab.rcParams['font.sans-serif'] = ['FangSong']  # 指定默认字体
pylab.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

def fibo(n):
    if n==1:
        return [1]
    elif n==2:
        return [1,1]
    #n>2
    series = [1,1]
    #First two menbers of the series

    for i in range(1,n-1):
        series.append(series[i]+series[i-1])
    return series

def draw_graph(x,y):
    plt.plot(x,y)
    plt.ylabel('比例')
    plt.title('斐波那契数列相邻数的比例')

def ratio(s,n):
    r = []
    x = []
    for i in range(1,n):
        r.append(s[i]/s[i-1])
        x.append(i)
    draw_graph(x,r)

if __name__ == '__main__':
    #print(fibo(20))

    try:
        l = input("请输入想查看的位数：")
        l = int(l)
    except ValueError:
        print("输入了不匹配字符:{}".format(l))
    else:
        f = fibo(l)
        print(f)

        ratio(f,l)
        plt.show()